﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;

namespace CommunitySystem
{
    public partial class TongZhiDanControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private TongZhiModel RoleModel = new TongZhiModel();
        private int OrgID = 0;
        public TongZhiDanControl()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Load();
        }

        public void Load()
        {
            Client.GetTongZhiModelCompleted += new EventHandler<GetTongZhiModelCompletedEventArgs>(Client_GetTongZhiModelCompleted);
            Client.GetTongZhiModelAsync(DateTime.Today.Year - nian.SelectedIndex, 0);
        }

        void Client_GetTongZhiModelCompleted(object sender, GetTongZhiModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            TongZhiDanWindow RMWindow = new TongZhiDanWindow(0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as TongZhiModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                TongZhiDanWindow RMWindow = new TongZhiDanWindow( Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的通知！"); 
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as TongZhiModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.DeteleTongZhiModelCompleted += new EventHandler<DeteleTongZhiModelCompletedEventArgs>(Client_DeteleTongZhiModelCompleted);
                Client.DeteleTongZhiModelAsync(Convert.ToInt32(seljw.ID));
            }
        }

        void Client_DeteleTongZhiModelCompleted(object sender, DeteleTongZhiModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
    }
}
